#include "../sys/sys_output.h"
#include "resource.h"

#include <windows.h>

#define CONSOLE_CLASSNAME "Game Console"
#define CONSOLE_TITLE "Console"

static HWND con_hWnd;
static HWND con_hWndBuf;

static LRESULT WINAPI con_WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    return DefWindowProc( hWnd, uMsg, wParam, lParam );
}


void sys_ErrorMessage(char *txt)
{
	MessageBox(NULL, txt, "ERROR", MB_OK | MB_ICONERROR);
	exit(0);
}

bool sys_createConsoleWindow()
{
	//the app instance
	HINSTANCE hInstance = GetModuleHandle(NULL);

	//register window class
	WNDCLASS wc;
	memset( &wc, 0, sizeof( wc ) );
	wc.style         = 0;
	wc.lpfnWndProc   = (WNDPROC) con_WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = hInstance;
	wc.hIcon         = NULL;
	wc.hCursor       = LoadCursor (NULL,IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = CONSOLE_CLASSNAME;

	if (!RegisterClass(&wc))
		return false;

	//position the window
	HDC hDC = GetDC(GetDesktopWindow());
	int pw = 650;
	int ph = 400;
	int px = GetDeviceCaps(hDC, HORZRES) - pw;
	int py = 0;
	ReleaseDC(GetDesktopWindow(), hDC);

	//create the window
	con_hWnd = CreateWindowEx( WS_EX_TOOLWINDOW,
							   CONSOLE_CLASSNAME,
							   CONSOLE_TITLE,
							   WS_POPUPWINDOW | WS_CAPTION,
							   px, py, pw, ph,
							   NULL,
							   NULL,
							   hInstance,
							   NULL );

	if (con_hWnd == NULL)
		return false;


	//create the edit control
	RECT r;
	GetClientRect(con_hWnd, &r);
	con_hWndBuf = CreateWindow( "edit", 
								NULL, 
								WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER | 
								ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY,
								r.left, r.top, r.right-r.left, r.bottom-r.top,
								con_hWnd, 
								NULL,
								hInstance, 
								NULL );

	//create the font and attach it to the control
	hDC = GetDC(con_hWnd);
	HFONT font = CreateFont( -MulDiv( 8, GetDeviceCaps( hDC, LOGPIXELSY), 72),
						  0,
						  0,
						  0,
						  FW_LIGHT,
						  0,
						  0,
						  0,
						  DEFAULT_CHARSET,
						  OUT_DEFAULT_PRECIS,
						  CLIP_DEFAULT_PRECIS,
						  DEFAULT_QUALITY,
						  FF_MODERN | FIXED_PITCH,
						  "Courier New" );
	ReleaseDC(con_hWnd, hDC);
	SendMessage( con_hWndBuf, WM_SETFONT, (WPARAM)font, 0 );

	//show the window
	ShowWindow(con_hWnd, SW_SHOWDEFAULT);
	UpdateWindow(con_hWnd);
	SetForegroundWindow(con_hWnd);

	//yeee, all ok
	return true;
}

void sys_addConsoleWindowText(const char *header, const char *text)
{
	//send the time & date
	SendMessage( con_hWndBuf, EM_LINESCROLL, 0, 0xffff);
	SendMessage( con_hWndBuf, EM_SCROLLCARET, 0, 0 );
	SendMessage( con_hWndBuf, EM_REPLACESEL, 0, (LPARAM) header );

	//parse the text, searching for newlines and converting them to \r\n
	char buf[4096];
	int p = 0;
	int i=0;

	while (text[i])
	{
		if (text[i]=='\n')
		{
			buf[p++]='\r';
			buf[p++]='\n';
			i++;
		}
		else
		{
			if (text[i]=='\r')
			{
				buf[p++]='\r';
				buf[p++]='\n';

				if (text[i+1] == '\n')
					i+=2;
				else
					i++;
			}
			else
				buf[p++] = text[i++];
		}
	}

	buf[p] = 0;

	//send the text
	SendMessage( con_hWndBuf, EM_LINESCROLL, 0, 0xffff);
	SendMessage( con_hWndBuf, EM_SCROLLCARET, 0, 0 );
	SendMessage( con_hWndBuf, EM_REPLACESEL, 0, (LPARAM) buf );
}

void sys_closeConsoleWindow()
{
	if ( con_hWnd )
	{
		//close the window
		ShowWindow( con_hWnd, SW_HIDE );
		CloseWindow( con_hWnd );
		DestroyWindow( con_hWnd );
		con_hWnd = NULL;
	}
}